package com.yvon.core.page;

import com.baomidou.mybatisplus.core.metadata.IPage;

public class ListData<T> extends BaseListData<T> {

    private final long FIRST_PAGE = 1L;

    protected IPage page;

    public ListData(IPage<T> page) {
        this.page = page;
    }

    @Override
    public long getPage() {
        return this.page.getCurrent();
    }

    @Override
    public long getSize() {
        return this.page.getSize();
    }

    @Override
    public long getTotalPages() {
        return this.page.getPages();
    }

    @Override
    public long getTotalElements() {
        return this.page.getTotal();
    }

    @Override
    public Boolean getHasPrevious() {
        return 1L != this.page.getCurrent();
    }

    @Override
    public Boolean getHasNext() {
        return this.page.getCurrent() < this.page.getPages();
    }

    @Override
    public Boolean isFirst() {
        return 1L == this.page.getCurrent();
    }

    @Override
    public Boolean isLast() {
        return this.page.getCurrent() == this.page.getPages();
    }

    public static ListData of(IPage page) {
        ListData listData = new ListData(page);
        listData.setList(page.getRecords());
        return listData;
    }
}
